package com.hansen.library.ui.widget.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.hansen.library.R;

/**
 * @author han
 * @date 2019/11/15
 */
public class AutoLoadMoreListView extends ListView {
    //防止多次访问网络
    private boolean mIsLoading = false;
    //是否还有数据
    private boolean mHasMore = false;
    private OnLoadMoreListener mOnLoadMoreListener;
    private View mHeaderView;

    public AutoLoadMoreListView(Context context) {
        super(context);
        init(context);
    }

    public AutoLoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mHeaderView = View.inflate(context, R.layout.layout_header_listview_progress, null);
        super.addHeaderView(mHeaderView);
        hideHeadView();
        super.setOnScrollListener(mScrollListener);
    }

    private void hideHeadView() {
        mIsLoading = false;
        mHeaderView.setVisibility(View.GONE);
    }

    private void showHeadView() {
        mIsLoading = true;
        mHeaderView.setVisibility(View.VISIBLE);
    }

    /**
     * 加载完数据，供外界调用
     */
    public void onLoadMoreComplete(boolean mHasMore) {
        this.mHasMore = mHasMore;
        mIsLoading = false;
        hideHeadView();
    }


    private AbsListView.OnScrollListener mScrollListener = new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
//            if (scrollState == SCROLL_STATE_FLING || scrollState == SCROLL_STATE_TOUCH_SCROLL || scrollState == SCROLL_STATE_IDLE) {
//                // 是否到了第一个条目
//                if (view.getFirstVisiblePosition() == 0 && !mIsLoading && mOnLoadMoreListener != null) {
//                    showHeadView();
//                    mOnLoadMoreListener.onLoadMore();
//                }
//            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0 && !mIsLoading && mHasMore && mOnLoadMoreListener != null) {
                showHeadView();
                mOnLoadMoreListener.onLoadMore();
            }
        }
    };

    /**
     * 加载更多数据的接口
     */
    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        mOnLoadMoreListener = onLoadMoreListener;
    }

    // 重新设置ListView的Stack
    public void resetListViewStack() {
        int count = getChildCount();
        View firstView = getChildAt(0);
        View lastView = getChildAt(count - 1);
        if (firstView != null && lastView != null) {
            int listViewPadding = getListPaddingBottom() + getListPaddingTop();
            int childViewsHeight = lastView.getBottom() - (firstView.getTop() == -1 ? 0 : firstView.getTop());
            int listViewHeight = getBottom() - listViewPadding;
            if (childViewsHeight < listViewHeight) {
                setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
                setStackFromBottom(false);
            } else {
                setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
            }
        }
    }
}
